<?php
/**
 * Pert Library
 * Uploaded File Class
 *
 * @author	Emre Hasegeli <hasegeli@gmail.com>
 * @license	GNU General Public License v3
 */

namespace Pert;

class FileUploadException extends FileException
{
	public static function Error($errorId)
	{
		if($errorId == 1)
			return new self('The uploaded file exceeds the upload_max_filesize directive in php.ini.', $errorId);

		if($errorId == 2)
			return new self('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.', $errorId);

		if($errorId == 3)
			return new self('The uploaded file was only partially uploaded.', $errorId);

		if($errorId == 4)
			return new self('No file was uploaded.', $errorId);

		if($errorId == 5)
			return new self('Missing a temporary folder.', $errorId);

		if($errorId == 6)
			return new self('Failed to write file to disk.', $errorId);

		if($errorId == 7)
			return new self('File upload stopped by extension.', $errorId);
	}
}

class UploadedFile implements iFile
{
	private $formInputName;
	protected $name;
	protected $extension;
	protected $size;

	public function __construct($formInputName, $name = NULL, $extension = NULL)
	{
		$this->formInputName = $formInputName;

		if(!$name)
		{
			$extension = substr($_FILES[$this->formInputName]['name'], strrpos($_FILES[$this->formInputName]['name'], '.') + 1);

			$name = substr($_FILES[$this->formInputName]['name'], 0, strrpos($_FILES[$this->formInputName]['name'], '.'));
		}
		if(!$extension)
		{
			$extension = substr($name, strrpos($name, '.') + 1);

			$name = substr($name, 0, strrpos($name, '.'));
		}

		$this->name = File::NameCheck($name);

		$this->extension = File::ExtensionCheck($extension);

		$this->size = $_FILES[$this->formInputName]['size'];

		$this->exception();
	}

	public function __destruct()
	{
		unset($_FILES[$this->formInputName]);
	}

	public function __toString()
	{
		return $this->name . '.' . $this->extension;
	}

	public function Name()
	{
		return $this->name;
	}

	public function Extension()
	{
		return $this->extension;
	}

	static $minSize = 0;

	final private function exception()
	{
		if($_FILES[$this->formInputName]['error'])
			throw FileUploadException::Error($_FILES[$this->formInputName]['error']);

		if($this->size <= static::$minSize)
			throw new FileUploadException('The uploaded file is too small.');

		$this->typeException($_FILES[$this->formInputName]['type']);
	}

	protected function typeException($type)
	{
		if($type == 'application/octet-stream')
			throw new FileUploadException('The uploaded file is not an application.');
	}

	final public function Size()
	{
		return $this->size;
	}

	protected static $object = 'Pert\File';

	public function Move($directory)
	{
		if(move_uploaded_file($_FILES[$this->formInputName]['tmp_name'],
				$directory . $this->name . '.' . $this->extension))
			return new static::$object($directory,
					$this->name,
					$this->extension);

		throw new FileUploadException('The file can not moved.');
	}

	public function Rename($name)
	{
		$this->name = File::NameCheck($name);
	}

	public function Delete()
	{
		if(unlink($_FILES[$this->formInputName]['tmp_name']))
			unset($this);

		throw new FileUploadException('The file can not deleted.');
	}

	public static function Select($formInputName)
	{
		if(is_uploaded_file($_FILES[$formInputName]['tmp_name']))
			return new self($formInputName, $name = NULL, $extension = NULL);
	}
}
